package com.hx.learn.config.autoconfigure;

import com.hx.learn.config.UserConfigConstants;
import com.hx.learn.config.properties.UserConfigProperties;
import com.hx.learn.config.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述：  自动注入什么东西
 *
 * @author Minghua Chen
 * @date 2020-10-04 22:16
 */
@Configuration
@EnableConfigurationProperties(value = UserConfigProperties.class)
@ConditionalOnClass(UserInfoService.class)
@ConditionalOnProperty(prefix = UserConfigConstants.PREFIX, matchIfMissing = true)
public class UserConfigAutoConfiguration {

    /**
     * 这里注入配置
     */
    @Autowired
    private UserConfigProperties userConfigProperties;

    /**
     *  将配置信息赋值给service 后面的业务可以直接获取
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(UserInfoService.class)
    public UserInfoService userInfoService(){
        UserInfoService userInfoService = new UserInfoService();
        userInfoService.setUserInfo(userConfigProperties.getId()+":"+userConfigProperties.getName());
        return userInfoService;
    }

}
